import os



def get_txt_files_in_directory(directory):
    txt_files = []
    for filename in os.listdir(directory):
        if filename.endswith(".txt") and os.path.isfile(os.path.join(directory, filename)):
            txt_files.append(filename)
    return txt_files

def extract_chapter_number(filename):
    try:
        # 从文件名中提取"第X章"的数字部分，并转换为整数
        chapter_str = filename.split("第")[1].split("章")[0].strip()
        return int(chapter_str)
    except (IndexError, ValueError):
        return float('inf')

current_directory = os.getcwd()
txt_files = get_txt_files_in_directory(current_directory)
txt_files.sort(key=extract_chapter_number)  # 使用自定义排序键
print(txt_files)
# 获取文件列表
# file_list = [
#     "《盗墓笔记》番外连载时间来了.txt",
#     "第401章 大结局（上） 吴邪心中的另一个人.txt",
#     "第402章 大结局（上） 由内而外的破绽.txt",
#     # 在此继续添加其他文件
# ]
#
# # 指定输出文件名
# output_file = "光阴之外.txt"
# output_file = "灵境行者.txt"
# output_file = "灵境行者.txt"
# output_file = "大王饶命.txt"
output_file = "武动乾坤.txt"
# 打开输出文件以写入内容
with open(output_file, "w", encoding="utf-8") as output:
    # 循环遍历每个文件并将内容写入输出文件
    for filename in txt_files:
        with open(filename, "r", encoding="utf-8") as input_file:
            content = input_file.read()
            output.write(content + "\n")  # 添加换行分隔
